package Peer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//import Database.TrackerDBHandler;
import FileStructure.FileHandler;
import FileStructure.Response;
import Peer.peerRequest.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.PeerEntity;

/**
 *
 * @author user
 */
public class PeerDownloads implements Runnable {


    public PeerEntity Peer;
    public PeerListener Listener;
    public PeerSender Sender;
    public Response response = new Response();
    public String dir;
    public PeerDownloads(PeerEntity pe){
        this.Peer = pe;
    }

    public void sendMessage(String msg){
        try {

            PrintWriter out = new PrintWriter(Peer.peerSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(Peer.peerSocket.getInputStream()));
            out.println(msg);
//            out.close();
//            in.close();
//            this.server.close();

        } catch (IOException ex) {
            Logger.getLogger(PeerDownloads.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void receiveMessage(){
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(Peer.peerSocket.getInputStream()));
            if ((this.Peer.message = in.readLine()) != null) {
                System.out.println(this.Peer.message);
            }
        } catch (IOException ex) {
            Logger.getLogger(PeerDownloads.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    //if download completed, Peer.request.event = stopped & Peer.downloadComplete = true
    public void run() {
       // throw new UnsupportedOperationException("Not supported yet.");
        PeerEntity otherPeer = new PeerEntity("127.0.0.1",1235,true);
        this.Peer.request.file_name="laguku.mp3";
        this.Listener = new PeerListener(this.Peer,otherPeer);
        //ntar si other peer didapet dari file response
        this.Sender = new PeerSender(otherPeer, this.Peer);
        this.Sender.dir = this.dir;
        this.Sender.peer.dir = this.Sender.dir;
        this.Sender.listener.dir = this.Sender.dir;
        
        Thread listener = new Thread(this.Listener);
        Thread sender = new Thread(this.Sender);
        //while(true){
            listener.start();
            sender.start();
       // }
        //System.out.println("Current download = " +this.Peer.request.file_name);
        
    }

//    public static void main(String[] args){
//        try {
//            PeerEntity peer1 = new PeerEntity("127.0.0.1", 1234, true);
//            PeerDownloads pd = new PeerDownloads(peer1);
//            PeerRequest pr = new PeerRequest(peer1);
//            pr.dir ="test/";
//            pd.dir ="test/";
//            Thread td = new Thread(pd);
//            Thread tr = new Thread(pr);
//            tr.start();
//            tr.sleep(1000);
//            td.start();
//            //tr.start();
//        } catch (InterruptedException ex) {
//            Logger.getLogger(PeerDownloads.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
//
//    
}
